// 1、导入创建切片对象，配置容器
import { createSlice, configureStore } from '@reduxjs/toolkit'

///2、创建slice对象
let countSlice = createSlice({
    // 设置别名
    name: 'count',
    // 初始化
    initialState: {
        value: 10
    },
    // 配置 reducer 的功能
    reducers: {
        add: (state, action) => {
            state.value += action.payload;
        },
        sub: (state, action) => {
            state.value -= action.payload;
        }
    }
});

// 3、配置store对象
let store = configureStore({
    reducer: countSlice.reducer
});

// 4、获得创建 action 对象的函数
export let { add, sub } = countSlice.actions;

// 5-1、修改状态
store.dispatch(add(1)); // +1 结果： 10 + 1 = 11
console.log(store.getState());
store.dispatch(sub(2)); // -2 结果： 11 - 2 = 9
console.log(store.getState());

// 异步方法
export function asyncAddAction(payload) {
    return dispatch => {
        setTimeout(() => {
            dispatch(add(10));
        }, 1000);
    }
}







export default store;